//
//  CurrentLoginUser.m
//  Kaagad Peso
//
//  Created by ZhengChengFeng on 2024/10/15.
//

#import "CurrentUser.h"

@implementation CurrentUser

static CurrentUser *_currentUser = nil;

+ (instancetype)mine{
    if (nil == _currentUser) {
        _currentUser = [[super alloc] initMine];
    }
    return _currentUser;
}

- (NSString *)sessionKey{
    if(!_sessionKey)
        return @"";
    return _sessionKey;
}

- (instancetype)initMine{
    self = [super init];
    if (self) {
        NSString * sessionKey = [[NSUserDefaults standardUserDefaults] objectForKey:kUserTokenSessionKey];
        if(!PMStringIsEmpty(sessionKey)){
            self.sessionKey = sessionKey;
        }
    }
    return self;
}

- (void)reset{
    if (_currentUser) {
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:kUserTokenSessionKey];
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:kNeedToRefreshOrderListPage];
        [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:kRecordBtnNum];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationLogoutSucess object:nil];
        _currentUser = nil;
        [[self class] mine];
    }
}

MJExtensionCodingImplementation
- (void)save{
    [NSThread sleepForTimeInterval:0.25f];
    [[NSUserDefaults standardUserDefaults] setObject:self.sessionKey forKey:kUserTokenSessionKey];
}


+ (void)loginSuccess:(NSString *)sessionKey{
    [[CurrentUser mine] setSessionKey:sessionKey];
    [[CurrentUser mine] save];
}

- (BOOL)hasLogged{
    if (!PMStringIsEmpty(self.sessionKey)) {
        return YES;
    }
    return NO;
}

- (BOOL)isYNLanguage{
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:kCurrentLanguageCode] integerValue] == 500 ) {
        return YES;
    } else {
        return NO;
    }
}

+ (void)logoutControllerWithIsTokenExpire:(BOOL)isTokenExpire{
    
    
    
    /*
    LoginViewController *login = [[LoginViewController alloc]init];
    UINavigationController *naV = [[UINavigationController alloc] initWithRootViewController:login];
    [UIApplication sharedApplication].keyWindow.rootViewController = naV;
    */
    
    [[CurrentUser mine] reset];
    
    [DKCore sharedDKCore].mainTabbarController = [[MainTabBarController alloc]init];
    
    [UIApplication sharedApplication].keyWindow.rootViewController = [DKCore sharedDKCore].mainTabbarController;
    
    if (isTokenExpire) {
        [[DKCore sharedDKCore] showTokenExpireAlert:isTokenExpire];
    }
    
    
}

+ (void)actionWhenLogin:(VoidBlock)block onVC:(UIViewController *)vc {
    
    if ([[CurrentUser mine] hasLogged]) {
        if (block) {
            block();
        }
    } else {
        [[DKCore sharedDKCore] loginController:^(void){
            if (block) {
                block();
            }
        }];
    }
}

+ (void)actionWhenLogin:(VoidBlock)block{
//    [self actionWhenLogin:block onVC:nil];
    
    if ([[CurrentUser mine] hasLogged]) {
        if (block) {
            block();
        }
    } else {
        [[DKCore sharedDKCore] loginController:^(void){
            
        }];
    }
    
}

@end
